/* ***********************************************************************
 * 项目名称:油耗
 * 项目组:
 * 创建时间:2017.7.13
 * 创建人:马方印
 * 用途:导航条上的时间选择页面
 * 版本:1.0
 * 修改记录:    修改内容   修改原因   修改人   修改时间
 *               无        无       无       无
 *************************************************************************/

#define calendarWidth  [UIScreen mainScreen].bounds.size.width
#define calendarHeight [UIScreen mainScreen].bounds.size.height

#import "ChooseDateView.h"
#import "SZCalendarPicker.h"

@interface ChooseDateView() {
    
    UIButton *_tempBtn;
    
    //开始时间
    NSString *_beginTime;
    //结束时间
    NSString *_endTime;
}

@property (nonatomic,strong) SZCalendarPicker *calenderPicker;

/**时间*/
@property (nonatomic , strong) NSString * timeString;

@end

@implementation ChooseDateView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        //默认今天时间
        _beginTime = [CommClass getTodayDateStr];
        _endTime = [CommClass getTodayDateStr];
        
        _timeString = [CommClass formatTimeStamp:[NSDate date] timeFormat:NEW_TIME_FORMATTER_DATE];
        
        [self crateSubViews];
    }
    
    return self;
}

#pragma mark 确定按钮监听
- (void)commitButtonClick:(UIButton *)sender{
    
    NSDate *startDate = [CommClass dateFormatTimeStamp:_beginTime timeFormat:@"yyyy-MM-dd"];
    NSDate *endDate =  [CommClass dateFormatTimeStamp:_endTime timeFormat:@"yyyy-MM-dd"];
    
    if ([endDate compare:startDate] == NSOrderedAscending) {
        
        [LZXHelper postMessageWithText:@"结束时间不能小于开始时间" View:self.window];
        
        return;
    }
    
    if (_selectDateCompletionHandle) {
        
        _selectDateCompletionHandle(_beginTime, _endTime);
    }
    
    [self setHidden:YES];
}

#pragma mark 取消按钮监听 
- (void)cancelButtonClick:(UIButton *)sender{
    
    [self setHidden:YES];
}

#pragma mark 清除按钮监听
- (void)clearButtonClick{
    
    UIButton *currentCalendarButton = (UIButton *)[self viewWithTag:200];
    currentCalendarButton.selected = YES;
    _tempBtn.selected = NO;
    
    //重置按钮为初始状态
    UIButton *startButton = (UIButton *)[self viewWithTag:1000];
    UIButton *endButton = (UIButton *)[self viewWithTag:2000];
    [startButton setTitle:@"选择开始时间" forState:UIControlStateNormal];
    [endButton setTitle:@"选择结束时间" forState:UIControlStateNormal];
    [startButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [endButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    
    _beginTime = @"";
    _endTime = @"";
}

#pragma mark 选择当天日期
- (void)currentCalendarButtonClick:(UIButton *)sender{
    
    UIButton *currentCalendarButton = (UIButton *)[self viewWithTag:200];
    currentCalendarButton.selected = NO;
    
    _tempBtn.selected = NO;
    sender.selected = YES;
    
    //重置按钮为初始状态
    UIButton *startButton = (UIButton *)[self viewWithTag:1000];
    UIButton *endButton = (UIButton *)[self viewWithTag:2000];
    [startButton setTitle:@"选择开始时间" forState:UIControlStateNormal];
    [endButton setTitle:@"选择结束时间" forState:UIControlStateNormal];
    [startButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [endButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    
    _beginTime = [LZXHelper getDayToday];
    _endTime = [LZXHelper getDayToday];
}

#pragma mark 日期选择按钮监听
- (void)selectBtnClick:(UIButton *)sender{
    
    UIButton *currentCalendarButton = (UIButton *)[self viewWithTag:200];
    currentCalendarButton.selected = NO;
    
    _tempBtn.selected = NO;
    _tempBtn = sender;
    sender.selected = YES;
    
    switch (sender.tag) {
        case 100://今天
            
            _beginTime = [LZXHelper getDayToday];
            _endTime = [LZXHelper getDayToday];
            
            break;
        case 101://本周
            
            _beginTime = [LZXHelper getFirstWeekTime];
            _endTime = [LZXHelper getLastWeekTime];
            
            break;
        case 102://本月
            
            _beginTime = [LZXHelper getFirstDayInMonth];
            _endTime = [LZXHelper getLastDayInMonth];
            
            break;
        case 103://上月
            
            _beginTime = [LZXHelper getFirstDayInLastMonth];
            _endTime = [LZXHelper getLastDayInLastMonth];
            
            break;
        case 104://本季
            
            _beginTime = [LZXHelper getFirstDayWithQuarter];
            _endTime = [LZXHelper getLastDayWithQuarter];
            
            break;
        case 105://本年
            
            _beginTime = [LZXHelper getFirstDayWithFirstHalfAYear];
            _endTime = [LZXHelper getLastDayWithSecondHalfAYear];
            
            break;
            
        default:
            break;
    }
    
    UIButton *startButton=(UIButton *)[self  viewWithTag:1000];
    UIButton *endButton=(UIButton *)[self viewWithTag:2000];
    [startButton setTitle:_beginTime forState:UIControlStateNormal];
    [startButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [endButton setTitle:_endTime forState:UIControlStateNormal];
    [endButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
}

#pragma mark 自定义时间选择按钮监听
- (void)selectorDateButtonClick:(UIButton *)sender{
    
    //日期选择页面
    _calenderPicker= [SZCalendarPicker showOnView:kWindow];
    _calenderPicker.today = [NSDate date];
    _calenderPicker.date = [CommClass dateFormatTimeStamp:_timeString timeFormat:NEW_TIME_FORMATTER_DATE];
    _calenderPicker.frame = CGRectMake(0, 0, kScreenWidth, 370);
    //选择日期的回调函数
    _calenderPicker.calendarDateBlock = ^(NSDate * date){
        
        NSString *timeStr = [CommClass formatTimeStamp:date timeFormat:NEW_TIME_FORMATTER_DATE];
        
        [sender setTitle:timeStr forState:UIControlStateNormal];
        [sender setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        
        if (sender.tag == 1000) {//开始时间
            
            _beginTime = timeStr;
            
        }else if (sender.tag == 2000){//结束时间
            
            _endTime = timeStr;
        }
        
        //若用户选择了自定义时间  则需要将所有非自定义按钮重置为未选中
        _tempBtn.selected = NO;
    };
}

// SZCalendarPickerDelegate
- (void)didSelectDayWithYear:(NSInteger)y month:(NSInteger)m day:(NSInteger)d week:(NSInteger)w tag:(NSInteger)tag{
    
    UIButton *currentCalendarButton = (UIButton *)[self viewWithTag:200];
    currentCalendarButton.selected = NO;
    _tempBtn.selected = NO;
    
    NSLog(@"%@",[NSString stringWithFormat:@"%li-%li-%li", (long)y, (long)m, (long)d]);
    
    NSString *date = [NSString stringWithFormat:@"%li-%li-%li", (long)y, (long)m, (long)d];
    
    if (tag == 1000) {//开始时间
        
        _beginTime = date;
        
    }else if (tag == 2000){//结束时间
        
        _endTime = date;
    }
    
    UIButton *startButton = (UIButton *)[self  viewWithTag:tag];
    [startButton setTitle:date forState:UIControlStateNormal];
    [startButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    
}

#pragma mark 创建视图
- (void)crateSubViews{
    
    //button with and height
    CGFloat margin = 10;
    CGFloat perWidth = (calendarWidth - 2 * margin - 10 * 2) / 3;
    CGFloat perHeight = 45;
    
    //白色背景
    
    //查询时间
    UILabel *queryTitleLabel = [[UILabel alloc]init];
    queryTitleLabel.frame = CGRectMake(10, 64, 230, 50);
    queryTitleLabel.text = @"查询时间";
    queryTitleLabel.textColor = [UIColor darkGrayColor];
    queryTitleLabel.font = [UIFont systemFontOfSize:18];
    [self addSubview:queryTitleLabel];
    
    //日期按钮
    NSArray *btnArray = @[@"今天",@"本周",@"本月",@"上月",@"本季度",@"本年"];
    
    for (int i = 0; i< btnArray.count; i++) {
        
        UIButton *btn = [[UIButton alloc]init];
        btn.frame = CGRectMake(10 + i%3 * (margin + perWidth), CGRectGetMaxY(queryTitleLabel.frame) + i/3 * (margin + perHeight), perWidth, perHeight);
        [btn setTitle:btnArray[i] forState:UIControlStateNormal];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 4;
        btn.layer.borderColor = [UIColor darkGrayColor].CGColor;
        btn.layer.borderWidth = 0.5f;
        btn.tag = i+100;
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage imageNamed:@"icon_button"] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
    }
    
    //自定义时间
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.frame = CGRectMake(10, perHeight*3 + 25 + 64, 230, 50);
    titleLabel.text = @"自定义时间";
    titleLabel.textColor = [UIColor darkGrayColor];
    titleLabel.font = [UIFont systemFontOfSize:18];
    [self addSubview:titleLabel];
    
    /*
     “至” 宽占50 清除按钮宽占50 两个选择按钮各占 （屏宽 - 50 - 50 - 边缘 * 2）/ 2
     */
    
    CGFloat btnY = CGRectGetMaxY(titleLabel.frame) - 5;
    CGFloat btnWidth = (calendarWidth - 50 - 30 - 15 * 2) / 2;
    CGFloat btnHeight = 45;
    
    //开始时间
    UIButton *startTimeBtn = [[UIButton alloc]init];
    startTimeBtn.frame = CGRectMake(10, btnY, btnWidth, btnHeight);
    [startTimeBtn setTitle:@"选择开始时间" forState:UIControlStateNormal];
    [startTimeBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    startTimeBtn.layer.masksToBounds = YES;
    startTimeBtn.layer.cornerRadius = 4.0f;
    startTimeBtn.layer.borderWidth = 0.5f;
    startTimeBtn.layer.borderColor = [UIColor darkGrayColor].CGColor;
    startTimeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    startTimeBtn.tag = 1000;
    [startTimeBtn addTarget:self action:@selector(selectorDateButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:startTimeBtn];
    
    //‘至’
    UILabel *zLabel = [[UILabel alloc]init];
    zLabel.frame = CGRectMake(CGRectGetMaxX(startTimeBtn.frame), btnY, 30, btnHeight);
    zLabel.text = @"至";
    zLabel.textAlignment = NSTextAlignmentCenter;
    zLabel.font = [UIFont systemFontOfSize:16];
    zLabel.textColor = [UIColor darkGrayColor];
    [self addSubview:zLabel];
    
    //结束时间
    UIButton *endTimeBtn = [[UIButton alloc]init];
    endTimeBtn.frame = CGRectMake(CGRectGetMaxX(zLabel.frame), btnY, btnWidth, btnHeight);
    [endTimeBtn setTitle:@"选择结束时间" forState:UIControlStateNormal];
    [endTimeBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    endTimeBtn.layer.masksToBounds = YES;
    endTimeBtn.layer.cornerRadius = 4.0f;
    endTimeBtn.layer.borderWidth = 0.5;
    endTimeBtn.layer.borderColor = [UIColor darkGrayColor].CGColor;
    endTimeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    endTimeBtn.tag = 2000;
    [endTimeBtn addTarget:self action:@selector(selectorDateButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:endTimeBtn];
    
    //清除按钮
    UIButton *clearBtn = [[UIButton alloc]init];
    clearBtn.frame = CGRectMake(CGRectGetMaxX(endTimeBtn.frame), btnY, 50, btnHeight);
    [clearBtn setTitle:@"清除" forState:UIControlStateNormal];
    clearBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [clearBtn setTitleColor:COLOR_MIDBLUE forState:UIControlStateNormal];
    [clearBtn addTarget:self action:@selector(clearButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:clearBtn];
    
    //取消按钮
    UIButton *cancelBtn = [[UIButton alloc]init];
    cancelBtn.frame = CGRectMake(25, kScreenHeight - 65, (kScreenWidth - 75)/2, 50);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:COLOR(51, 51, 51, 1) forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:18.0f];
    cancelBtn.layer.cornerRadius = 4.0f;
    cancelBtn.layer.masksToBounds = YES;
    cancelBtn.layer.borderWidth = 1.0f;
    cancelBtn.layer.borderColor = COLOR(121, 121, 121, 1).CGColor;
    [cancelBtn addTarget:self action:@selector(cancelButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];
    
    //确定按钮
    UIButton *commitBtn = [[UIButton alloc]init];
    commitBtn.frame = CGRectMake(50 + (kScreenWidth - 75)/2, kScreenHeight - 65, (kScreenWidth - 75)/2, 50);
    [commitBtn setTitle:@"确定" forState:UIControlStateNormal];
    [commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [commitBtn setBackgroundColor:COLOR(22, 155, 213, 1)];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:18.0f];
    commitBtn.layer.cornerRadius = 4.0f;
    commitBtn.layer.masksToBounds = YES;
    [commitBtn addTarget:self action:@selector(commitButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:commitBtn];
}

@end
